﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Furion;
using Furion.DynamicApiController;
using Furion.UnifyResult;
using LNSServer.Models;
using LNSServer.ViewModels.TestUseCase;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;

namespace LNSServer.Controllers
{
    /// <summary>
    /// 测试用例
    /// </summary>
    [DynamicApiController]
    public class LnsTestUseCaseController
    {
        private readonly ILogger<LnsTestUseCaseController> logger;
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        public LnsTestUseCaseController(ISqlSugarClient sqlSugarClient)
        {
            this.db = sqlSugarClient;
            this.logger = logger;
        }

        /// <summary>
        /// 测试用例列表
        /// </summary>
        /// <param name="tucsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> List([FromBody] TestUseCaseSearchForm tucsf)
        {
            RefAsync<int> total = 0;
            List<TestUseCase> testUseCases = await db.Queryable<TestUseCase>()
                .Mapper(dt => dt.User, dt => dt.UId, dt => dt.User.Id)
                .Where(dt => dt.IsActive == 1)
                .WhereIF(!string.IsNullOrEmpty(tucsf.Content), dt => dt.Name.Contains(tucsf.Content))
                .WhereIF(tucsf.UId != 0, dt => dt.UId == tucsf.UId)
                .ToPageListAsync(tucsf.PageIndex, tucsf.PageSize, total);

            UnifyContext.Fill(new { Total = total.Value });
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = testUseCases,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 增加测试用例
        /// </summary>
        /// <param name="tucsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Save([FromBody] TestUseCaseSaveForm tucsf)
        {
            await db.Insertable<TestUseCase>(tucsf.Adapt<TestUseCase>()).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = tucsf,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 删除测试用例
        /// </summary>
        /// <param name="tucdf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete([FromBody] TestUseCaseDeleteForm tucdf) {
            await db.Updateable<TestUseCase>(new TestUseCase { IsActive = 0 }).UpdateColumns(tuc => tuc.IsActive).Where(tuc => tuc.Id == tucdf.Id).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 修改测试用例
        /// </summary>
        /// <param name="tucuf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update([FromBody] TestUseCaseUpdateForm tucuf) {
            await db.Updateable<TestUseCase>(tucuf).ExecuteCommandAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = tucuf,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 查看测试用例
        /// </summary>
        /// <param name="tucsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Show([FromBody] TestUseCaseShowForm tucsf) {
            TestUseCase testUseCase = await db.Queryable<TestUseCase>()
                .InSingleAsync(tucsf.Id);
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = testUseCase,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

    }
}
